-
Notifications
You must be signed in to change notification settings - Fork 30
/
Connect-PSS.ps1
63 lines (51 loc) · 1.76 KB
/
Connect-PSS.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<#
$Metadata = @{
Title = "Connect Powershell Remote Session"
Filename = "Create-PSS.ps1"
Description = ""
Tags = "powershell, remote, session"
Project = ""
Author = "Janik von Rotz"
AuthorContact = "http://janikvonrotz.ch"
CreateDate = "2013-03-12"
LastEditDate = "2013-10-02"
Version = "3.0.0"
License = @'
This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/3.0/ or
send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
'@
}
#>
function Connect-PSS{
param (
[parameter(Mandatory=$true)]
[string[]]$Name,
[parameter(Mandatory=$false)]
[System.Management.Automation.Credential()]
$Credential = [System.Management.Automation.PSCredential]::Empty
)
#--------------------------------------------------#
# main
#--------------------------------------------------#
# Load Configurations
$Servers = Get-RemoteConnection -Name $Name
foreach($Server in $Servers){
# Delete Session if already opened
Remove-PSSession -ComputerName $Server.Name -ErrorAction SilentlyContinue
# Get connect account credentials
if($Credential.UserName -eq $null){
$Credential = Get-Credential $Server.User
}
# Open Session
$s = New-PSSession -Name $Server.Name -ComputerName $Server.Name -Credential $Credential
# Load SnapIns
if ($Server.SnapIns -ne ""){
foreach($SnapIn in $Server.SnapIns){
Invoke-Command -Session $s -ScriptBlock {param ($Name) Add-PSSnapin -Name $Name} -ArgumentList $SnapIn
}
}
$Sessions += $s
}
$Sessions
}